/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.henhouse.modular.henhouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.barn.modular.barn.entity.BizBarn;
import vip.xiaonuo.barn.modular.barn.mapper.BizBarnMapper;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.henhouse.modular.henhouse.entity.BizHenhouse;
import vip.xiaonuo.henhouse.modular.henhouse.mapper.BizHenhouseMapper;
import vip.xiaonuo.henhouse.modular.henhouse.param.BizHenhouseAddParam;
import vip.xiaonuo.henhouse.modular.henhouse.param.BizHenhouseEditParam;
import vip.xiaonuo.henhouse.modular.henhouse.param.BizHenhouseIdParam;
import vip.xiaonuo.henhouse.modular.henhouse.param.BizHenhousePageParam;
import vip.xiaonuo.henhouse.modular.henhouse.service.BizHenhouseService;
import vip.xiaonuo.henhouse.modular.henhouse.service.impl.util.TreeNode;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 鸡舍管理Service接口实现类
 *
 * @author zyb
 * @date  2023/11/17 11:14
 **/
@Service
@Slf4j
public class BizHenhouseServiceImpl extends ServiceImpl<BizHenhouseMapper, BizHenhouse> implements BizHenhouseService {

    @Resource
    private BizBarnMapper bizBarnMapper;

    @Override
    public Page<BizHenhouse> page(BizHenhousePageParam bizHenhousePageParam) {
        QueryWrapper<BizHenhouse> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizHenhousePageParam.getName())) {
            queryWrapper.lambda().like(BizHenhouse::getName, bizHenhousePageParam.getName());
        }
        if (ObjectUtil.isNotEmpty(bizHenhousePageParam.getState())){
            queryWrapper.lambda().like(BizHenhouse::getState,bizHenhousePageParam.getState());
        }
        if(ObjectUtil.isAllNotEmpty(bizHenhousePageParam.getSortField(), bizHenhousePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizHenhousePageParam.getSortOrder());
            queryWrapper.orderBy(true, bizHenhousePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizHenhousePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizHenhouse::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizHenhouseAddParam bizHenhouseAddParam) {
        BizHenhouse bizHenhouse = BeanUtil.toBean(bizHenhouseAddParam, BizHenhouse.class);
        this.save(bizHenhouse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizHenhouseEditParam bizHenhouseEditParam) {
        BizHenhouse bizHenhouse = this.queryEntity(bizHenhouseEditParam.getId());
        BeanUtil.copyProperties(bizHenhouseEditParam, bizHenhouse);
        this.updateById(bizHenhouse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizHenhouseIdParam> bizHenhouseIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizHenhouseIdParamList, BizHenhouseIdParam::getId));
    }

    @Override
    public BizHenhouse detail(BizHenhouseIdParam bizHenhouseIdParam) {
        return this.queryEntity(bizHenhouseIdParam.getId());
    }

    @Override
    public BizHenhouse queryEntity(Integer id) {
        BizHenhouse bizHenhouse = this.getById(id);
        if(ObjectUtil.isEmpty(bizHenhouse)) {
            throw new CommonException("鸡舍不存在，id值为：{}", id);
        }
        return bizHenhouse;
    }

    @Override
    public List<TreeNode> tree() {
        log.info("构造栏舍树...");
        // 获取所有的鸡舍
        List<BizHenhouse> allHenhouse = this.list();
        // 获取所有的栏舍
        List<BizBarn> allBran = bizBarnMapper.selectList(null);

        // 构造鸡舍-栏舍树
        List<TreeNode> treeData = new ArrayList<>();
        for (BizHenhouse henhouse : allHenhouse) {
            TreeNode treeNode = new TreeNode(henhouse.getName(),henhouse.getId().toString());
            for (BizBarn barn : allBran) {
                if (barn.getHenhouseId().equals(henhouse.getId())){
                    treeNode.addChild(new TreeNode(barn.getName(),barn.getId()));
                }
            }
            treeData.add(treeNode);
        }
        return treeData;
    }

    /**
     * 获取鸡舍列表
     *
     * @return {@link List}<{@link BizHenhouse}>
     */
    @Override
    public List<BizHenhouse> getList() {
        return this.list();
    }
}
